<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <button id="btn" type="button">点我打印</button>
</body>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
    $("#btn").click(function () {
        // 创建WebSocket 对象
        var ws = new WebSocket("wss://127.0.0.1:18653");
        //连接成功时，触发事件
        // 发送打印数据协议(print)
        ws.onopen = function () {
            //请求参数
            // var param = { "cmd": 'getPrinters', "requestID": "123458976", "version": "1.0" }
            var param = {
                "ERPId": 303379400,
                "cmd": "print",
                "requestID": "90E06437",
                "task": {
                    "documents": [
                        {
                            "contents": [
                                {
                                    "addData": {
                                        "sender": {
                                            "address": {
                                                "city": "上海市",
                                                "detail": "陕西面馆",
                                                "district": "长宁区",
                                                "province": "上海市"
                                            },
                                            "mobile": "333333333333",
                                            "name": "ceshi"
                                        }
                                    },
                                    "encryptedData": "a67d4819d0cd21554e9437e0bc2caa629864b0effd1aec65833023f78e93733ca9eec3468dc487b24682b9d64f176ca6504307461a33a112a71b2c1be2423de3a1d7ecdb81adba0ba405fca92cc5b1c814aaf39ae019f8fc00a1dec2b57516b0343692ef81a5f15638fa0bcef04bae8626d146d21a4e58b83bb1cfac198901e9b70bd049b8f9df8b4e58efdc06d8a25e07faf58bbfc8ace3d19088b8bbecf7b0d5a45cde7de8ef5096d1b147f5d01b9af7b6799bcf45c9ecd4ce0e8762177c082d3fde66c6ce4db98549a77360a9bf86ad78ad362b1586f71845c44585fd285ef3283f32ef9fd9976307a9b1f8c57335e02f3b5abcdca3e665f90dcf8578f35cf4b4fc577a8bf3201128b8e33c92642c785fae9e3db1885218ee686cc375401c62d180d1b003ccbf25b5d7ee664b5bf64081af153942ecfe9206dd6b99b7950d752d2c6ab3ed11dde4bff04c23f80f1b9b02d91ba995e81049f9b07fa9498c7ec09995055f3721189cd2b2ca77c8132ce34a300d172abbef240403de1edbdc6d01773339ca98d5e306ff18042640867602c2a3d4296f9b55e8f0153a280ae733f4a88abf8480d3e6b55b373dd06bc90d41b5fa76947f5fff499f31cb8f3853555a041cc368fda4ea7bae7bc84b1fc8ad398963c4aaf00a2e678f431f8ce4c93d78e135fc6cfbf4c10350561908e8affc7f7c30241f417af60020937d9f0679a4bf90ed8e0f732e2cd9331036b9aaea51dd3819c0b68e198cd91b65133cb1521817b82cf5751c8a75fd13b6d3c1cd5bc049cd82d6485af38595eb9049cf78eb95e463ca58d9e0b2f6a6ec33b6481e92226794233af3c01ea979e7e2fbc70912dca2d81bc2424666bae56b91a928ddf8b0d1982439b7e8cc0c272d246c2c7dd421a02b170db32ad336bd0899f4eb441148d755b89c098871995323c9b8e2dacea28496073948cca5941c6e410be95bc089846fe8f33ce03e737706457f3baea57588308ef6e60c268cc18ead9687b891bf901a5c17801fb2567500f0055b6729b60a2d6a90e60ab5dc1285cf3b0bc9324dcbe7f5ac268caa8078ea97fca1c3c7c6d07f1106ac7459c327ffc9d99a61fd192b3efe535eab48e57183ef4b2712eff40f17b92857f3a556a300d7fb2f691fb3574212fee91e258210f0b16f75effaa17588dd05e62e6a0523329fc45f5f9d6d888716c0b72980e8afa3dc93249f6fddadf78a7af3847f7cda19a8138ff7768bea7d7a6d6a6ac8c9565da1f4b9e899e584a20ef1a6905cece1f20de1eb84a0b521dae33bd8d78eb8704982d62b83699d",
                                    "signature": "D+lBjEEQeEAXA6S7qL7PfljZWYJee40zF0WMn2wq7taN9Y/CH1rX+Ch8EY2Ph3wN+GYbfqBjNb7wh8KARLdAfxCDJx4OatXhkwZeMVo1Sh27YWVW2Hk1yDid5ZSEo2i5eE0DqrbC/iXQAc10w+hSyogHN9VRCyltzFzGXDX9C6I=",
                                    "templateUrl": "https://file-link.pinduoduo.com/db_one",
                                    "userid": "pdd62521972682",
                                    "ver": "3"
                                },
                                {
                                    "data": {
                                        "height": 240,
                                        "list": [
                                            {
                                                "fontSize": 31.2,
                                                "height": 45.68,
                                                "left": 2.08,
                                                "text": "[备注: 没有什么备注]",
                                                "top": 2.08,
                                                "width": 413.52
                                            }
                                        ],
                                        "waterdata": {
                                            "text": ""
                                        },
                                        "width": 560
                                    },
                                    "templateURL": "https://file-link.pinduoduo.com/db_one"
                                }
                            ],
                            "documentID": "DPK368062829273"
                        }
                    ],
                    "notifyType": [
                        "print"
                    ],
                    "preview": false,
                    "previewType": "Image",
                    "printer": "QR-488 LABEL",
                    "taskID": "03281843"
                    // Date.parse( new Date())
                },
                "version": "1.0"
            }
            // 使用 send() 方法发送数据 ;
            ws.send(JSON.stringify(param));
            alert("数据发送中...");
        }
        //接收到服务端响应的数据时，触发事件
        ws.onmessage = function (evt) {
            var data = evt.data;
            console.log(evt)
            console.log(data)
        }
        // 断开 web socket 连接成功触发事件
        ws.onclose = function () {
            alert("连接已关闭...");
        };
    });
</script>

</html>